package com.itheima.pinda.web.transportline;

import com.itheima.pinda.DTO.transportline.TransportLineDto;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.common.utils.Result;
import com.itheima.pinda.entity.transportline.PdTransportLine;
import com.itheima.pinda.mapstruct.TransportDTO2Entity;
import com.itheima.pinda.service.PdTransportLineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Objects;

/**
 * 运输线路 管理模块
 *
 * @author llhuang10
 * @date 2021/10/6 19:35
 */
@Slf4j
@Validated
@RestController
@Api(tags = "运输线路模块")
@RequestMapping("/base/transportLine")
public class TransportLineController {

    @Resource
    private PdTransportLineService transportLineService;

    @Resource
    private TransportDTO2Entity transportDTO2Entity;

    /**
     * 添加线路
     *
     * @param dto 线路信息
     * @return 线路详情
     */
    @PostMapping
    @ApiOperation("添加线路")
    public TransportLineDto saveTransportLine(@Valid @RequestBody TransportLineDto dto) {
        return transportLineService.saveTransportLine(dto);
    }

    /**
     * 根据id获取线路详情
     *
     * @param id 线路id
     * @return 线路信息
     */
    @GetMapping("/{id}")
    @ApiOperation("添加线路")
    public TransportLineDto findById(@NotBlank(message = "线路id不能为空!") @PathVariable(name = "id") String id) {
        PdTransportLine pdTransportLine = transportLineService.getById(id);
        if (Objects.nonNull(pdTransportLine)) {
            return transportDTO2Entity.conver(pdTransportLine);
        } else {
            TransportLineDto transportLineDto = new TransportLineDto();
            transportLineDto.setId(id);
            return transportLineDto;
        }
    }

    /**
     * 获取线路分页信息
     *
     * @param page                页码
     * @param pageSize            页尺寸
     * @param lineNumber          线路编号
     * @param name                线路名称
     * @param transportLineTypeId 线路类型id
     * @return 线路分页信息
     */
    @GetMapping("/page")
    @ApiOperation("获取线路分页信息")
    public PageResponse<TransportLineDto> findByPage(@RequestParam(name = "page") Integer page,
                                                     @RequestParam(name = "pageSize") Integer pageSize,
                                                     @RequestParam(name = "lineNumber", required = false) String lineNumber,
                                                     @RequestParam(name = "name", required = false) String name,
                                                     @RequestParam(name = "transportLineTypeId", required = false) String transportLineTypeId) {
        return transportLineService.findByPage(page, pageSize, lineNumber, name, transportLineTypeId);
    }

    /**
     * 获取线路列表
     *
     * @param ids       线路id列表
     * @param agencyId  机构Id
     * @param agencyIds 机构Id列表
     * @return 线路列表
     */
    @GetMapping
    @ApiOperation("获取线路列表")
    public List<TransportLineDto> findAll(@RequestParam(name = "ids", required = false) List<String> ids,
                                          @RequestParam(name = "agencyId", required = false) String agencyId,
                                          @RequestParam(name = "agencyIds", required = false) List<String> agencyIds) {
        return transportLineService.findAll(ids, agencyId, agencyIds);
    }

    /**
     * 更新线路信息
     *
     * @param id  线路id
     * @param dto 线路信息
     * @return 线路信息
     */
    @PutMapping("/{id}")
    @ApiOperation("更新线路信息")
    public TransportLineDto update(@NotBlank(message = "线路Id不能为空") @PathVariable("id") String id,
                                   @Valid @RequestBody TransportLineDto dto) {
        dto.setId(id);
        PdTransportLine pdTransportLine = transportDTO2Entity.conver(dto);
        transportLineService.updateById(pdTransportLine);
        return dto;
    }

    /**
     * 删除线路
     *
     * @param id 线路id
     * @return 返回信息
     */
    @PutMapping("/{id}/disable")
    @ApiOperation("删除线路")
    public Result disable(@NotBlank(message = "线路Id不能为空") @PathVariable("id") String id) {
        PdTransportLine pdTransportLine = new PdTransportLine();
        pdTransportLine.setId(Long.parseLong(id));
        pdTransportLine.setStatus(Constant.DATA_DISABLE_STATUS.byteValue());
        boolean res = transportLineService.updateById(pdTransportLine);
        return res ? Result.ok() : Result.error("删除路线失败, 请联系管理员!");
    }


    /**
     * 获取线路列表
     *
     * @return 线路列表
     */
    @GetMapping("/list")
    @ApiOperation("获取线路列表")
    public List<TransportLineDto> getList(@RequestBody TransportLineDto transportLineDto) {
        return transportLineService.getList(transportLineDto);
    }

}
